import baseurl from './vmeitime-http/cofig.js'
import _GENV from '@/common/env.js'
var contentType=1;

	//开发环境调试
	//请把有效的token复制到下面
	// uni.setStorageSync('token','eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJhaS50dC5jb20iLCJhdWQiOiJpY29uX3BxcF9wIiwiaWF0IjoxNjI5MTAwOTY4LCJleHAiOjE2MjkxMTE3NjgsInVpZCI6Im85SElGajdlYS1YcUZnMVM1cjBJRWJYa2wyRkEifQ.xp8j9z-qD6KNGWvQIprKBuwyGEaV1tkuQKbg5fIdy04');
	//END





function tokelogin(){

	// #ifdef H5 
	var local = 'https://' + _GENV.Gdomain + '/wap/#/pages/shouquan/shouquan'; // 获取页面url
	// var appid = 'wxed93066ca98ccfb9'; //智服网
	var appid = _GENV.GwechatAppid; //迎客周
	var href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(
	local
	)}&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect`;
	window.location.replace(href);
	// #endif
	// #ifdef MP-WEIXIN
	
	// #endif
}					
function getrequest(url, data) {   //get 方法请求 
	   const token = uni.getStorageSync('token');
	   var header = {
	   	'token' :'',
	   	'content-type':contentType === 1 ? 'application/json' : 'application/x-www-form-urlencoded',
	   }
	  
	   const username = uni.getStorageSync('username');		
	   var httpshowpath=sessionStorage.getItem("httpshowpath");
	   if(!httpshowpath){
		   tokelogin(); //没有token跳转获取
		   return;
	   }
	   if (token) {
		   header.token=token
	   			// var header = {
	   			// 	'token' :token,
	   			// 	'content-type':contentType === 1 ? 'application/json' : 'application/x-www-form-urlencoded',
	   			// }
	     }else{
	   		  //  uni.switchTab({
	   			 // url: '/pages/main/main'
	   		  //  });
	   		  // tokelogin(); //没有token跳转获取
	   		  // return;
	   }
	  
	  const userid = uni.getStorageSync('userid');
	 //  if(!data.uid&&userid){
	 //  		   data.uid=userid;
	 //  }
	 //  if(userid){
		// data.userid=userid;
	 //  }
	  
    for (let property in data) {
        if (data[property] == null) {
            delete data[property]
        }
    }
	var method='get';
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseurl + url,
            data,
            method,
            header,
            success: (res) => {
				if (res.data.message == 'token已过期') {
					 uni.showToast({
					 	title: res.data.message,
					 	icon: 'none',
						duration: 2000
					 });
				
					tokelogin();//方法跳转
					 
				} else if (res.data.message == 'token不存在') {
					uni.showToast({
					 	title: res.data.message,
					 	icon: 'none',
						duration: 2000
					 });
					tokelogin();//方法跳转  
				}
				else if (res.data.message == '请关注公众号') {
					 uni.showModal({
					 	title: '',
					 	content: res.data.message,
					 	showCancel: false,
					 	confirmText: "确定",
					 	success: function(res) {
					 		if (res.confirm) {
								// //迎客舟
								// var href = 'https://mp.weixin.qq.com/s/jopUdv6ICw6uxN3XB_3Wnw'; 
								// 智服网	
								var href = 'https://mp.weixin.qq.com/s/kc64hSEoH7YCOHfDSY50Ng';	
								localStorage.setItem('restype',this.registerstate)
								window.location.replace(href)	
							} else if (res.cancel) {
					 			console.log('用户点击取消');
					 		}
					 	}
					 });
					 
					// tokelogin();//方法跳转  
				}else if (res.data.message == '用户不存在') {
					uni.showToast({
					 	title: res.data.message,
					 	icon: 'none',
						duration: 2000
					 });
					tokelogin();//方法跳转
				}else{
					resolve(res)
				}
            },
            fail: (err) => {
                console.log('request fail', err)
                uni.showToast({
                    icon: 'none',
                    title: '请求出错，请稍后再试',
                    duration: 2000
                });
                reject(err)
            }
        })
    })
}
function postrequest(url, data) {   //post 方法请求 
	   const token = uni.getStorageSync('token');
	   //const token = '8565d425d5baff8f43d9dbd201124dac20a88b35'  //临时 生产删除
	   const username = uni.getStorageSync('username');
	   var httpshowpath=sessionStorage.getItem("httpshowpath");
	   if(!httpshowpath){
	   		   tokelogin(); //没有token跳转获取
	   		   return;
	   }
	   if (token) {
	   			var header = {
	   				'token' :token,
	   				'content-type':contentType === 1 ? 'application/json' : 'application/x-www-form-urlencoded',
	   			}
	     }else{
	   		  //  uni.switchTab({
	   			 // url: '/pages/main/main'
	   		  //  });
	   		  tokelogin(); //没有token跳转获取
	   		  return;
	   }
	   
	  const userid = uni.getStorageSync('userid');
	  // if(!data.uid&&userid){
	  // 		   data.uid=userid;
	  // }
	  // if(userid){
	  // 		data.userid=userid;
	  // }
    for (let property in data) {
        if (data[property] == null) {
            delete data[property]
        }
    }
	var method='post';
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseurl + url,
            data,
            method,
            header,
            success: (res) => {
				if (res.data.message == 'token已过期') {
					 uni.showToast({
					 	title: res.data.message,
					 	icon: 'none',
						duration: 2000
					 });
					tokelogin();//方法跳转
					 
				} else if (res.data.message == 'token不存在') {
					uni.showToast({
					 	title: res.data.message,
					 	icon: 'none',
						duration: 2000
					 });
					tokelogin();//方法跳转
				} else if (res.data.message == '用户不存在') {
					uni.showToast({
					 	title: res.data.message,
					 	icon: 'none',
						duration: 2000
					 });
					tokelogin();//方法跳转
				}else{
					resolve(res)
				}
            },
            fail: (err) => {
                console.log('request fail', err)
                uni.showToast({
                    icon: 'none',
                    title: '请求出错，请稍后再试',
                    duration: 2000
                });
                reject(err)
            }
        })
    })
}


// 图片上传
function imguploadFile(url, data) {   //图片上传功能
		console.log(url);
	   const token = uni.getStorageSync('token');
	   
	   //const token = '8565d425d5baff8f43d9dbd201124dac20a88b35'  //临时 生产删除
	   const username = uni.getStorageSync('username');
	
	   if (token) {
	   			var header = {
	   				'token' :token,
	   				'content-type':contentType === 1 ? 'application/json' : 'application/x-www-form-urlencoded',
	   			}
	     }else{
	   		  //  uni.switchTab({
	   			 // url: '/pages/main/main'
	   		  //  });
	   		  tokelogin(); //没有token跳转获取
	   		  return;
	   }
	  
	  var _self = this;
	  return new Promise((resolve, reject) => {
		  if(data.choosetype=="face"){
			var showtype=['camera'];  
		  }else{
			 var showtype=['album', 'camera']; 
		  }
		  uni.chooseImage({
		    	count: 1,
		    	sourceType: showtype, //从相册选择
		    	success: res => {
		  		console.log(res)
		  		_self.idCard1 = res.tempFilePaths[0];
		  		console.log(res.tempFilePaths[0]);
		    		uni.showLoading({
		    			title: '上传中'
		    		});
				
				// 上传正面
				uni.uploadFile({
					url: baseurl+url,
					header: {
						token: token
					},
					filePath: _self.idCard1,
					fileType: 'image',
					name: 'imgurl',
					formData:data,
					success: (vifres) => {
						// 上传成功后返回服务器端保存的路径
						// console.log("getMachineNum success:" + JSON.stringify(res));
						// console.log('获取code' + JSON.parse(res.data));
						// this.data = JSON.parse(res.data);
						// console.log(this.data);
					  
						if (vifres.data.message == 'token已过期') {
							uni.hideLoading();
							console.log(vifres.data.message);
							uni.showToast({
								title: vifres.data.message,
								icon: 'none',
								duration: 2000
							});
					  
							tokelogin();//方法跳转
						} else if (vifres.data.message == 'token不存在') {
							uni.hideLoading();
							console.log(vifres.data.message);
							uni.showToast({
								title: vifres.data.message,
								icon: 'none',
								duration: 2000
							});
					  
							tokelogin();//方法跳转
						} else if (vifres.data.code == 400) {
							uni.hideLoading();
							console.log(vifres.data.message);
							uni.showToast({
								title: vifres.data.message,
								icon: 'none',
								duration: 2000
							});
						} else {
							uni.hideLoading();
							resolve(vifres)
						}
					}
				});
		  
		  	}
		  })

	  	});

}

export default {
	getrequest:getrequest,
	postrequest:postrequest,
	imguploadFile:imguploadFile
};